﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using Janus.Windows.GridEX;

namespace Company.Portal.Win
{
    public partial class LayoutSetting : Form
    {
        public LayoutSetting()
        {
            InitializeComponent();
            treeView1.NodeMouseClick += RightClickOnNode;

        }

        public GridEX GridEx { get; set; }
        private void RightClickOnNode(object sender, TreeNodeMouseClickEventArgs treeNodeMouseClickEventArgs)
        {
            if (treeNodeMouseClickEventArgs.Button == MouseButtons.Right)
            {
                ContextMenuStrip = new ContextMenuStrip();
                ContextMenuStrip.Items.Clear();
                ToolStripButton toolStripButton = new ToolStripButton();
                toolStripButton.Name = "Node";
                toolStripButton.Text = "حذف ستون از چینش";
                toolStripButton.Tag = treeNodeMouseClickEventArgs.Node;
                toolStripButton.Click += ServiceClicked;
                ContextMenuStrip.Items.Add(toolStripButton);
                ContextMenuStrip.Show(treeNodeMouseClickEventArgs.X, treeNodeMouseClickEventArgs.Y);
            }

        }List<XmlNode> deleteCol = new List<XmlNode>();
        List<TreeNode> noneDeleted = new List<TreeNode>();
        private void ServiceClicked(object sender, EventArgs eventArgs)
        {
            var toolStripB = sender as ToolStripButton;
            if (toolStripB != null)
            {
                var node = (TreeNode)toolStripB.Tag;
                if (node == null)
                    return;
                deleteCol.Add((XmlNode)node.Tag);
                noneDeleted.Clear();
                foreach (TreeNode treeNode in treeView1.Nodes[0].Nodes)
                    if (treeNode != node)
                        noneDeleted.Add(treeNode);
                treeView1.Nodes[0].Nodes.Clear();
                foreach (var treen in noneDeleted)
                {
                    treeView1.Nodes[0].Nodes.Add(treen);
                }
            }
        }
        public GridEXLayout Layout { get; set; }
        public BusinessObjectListControl ParentControl { get; set; }
        protected override void OnLoad(EventArgs e)
        {
           
            GetTreeLayout();
            base.OnLoad(e);
        }

        private XmlDocument xmlDocument;
        private void GetTreeLayout()
        {

            xmlDocument = new XmlDocument();
            xmlDocument.LoadXml(Layout.LayoutString);
            TreeNode treeNode = new TreeNode();
            foreach (XmlNode xml in xmlDocument)
            {
                //          var collumns=  GetCollumns(xmlDocument.ChildNodes);
                treeNode.Text = xml.Name;
                CreateTreeNodeFromXml(xml, treeNode);
            }
            treeView1.Nodes.Add(treeNode);

        }
        private void CreateTreeNodeFromXml(XmlNode xmlNode, TreeNode treeNode)
        {
            foreach (XmlNode childNode in xmlNode.ChildNodes)
            {
                if (childNode.Name == "Columns")
                {
                    foreach (XmlNode child in childNode.ChildNodes)
                    {
                        var nameCol = string.Empty;
                        foreach (XmlNode x in child)
                        {
                            if (x.Name == "Caption" && x.ChildNodes.Count != 0)
                            {
                                nameCol = x.InnerText;
                                break;
                            }
                        }
                        var tnode = new TreeNode(nameCol);

                        tnode.Tag = child;
                        treeNode.Nodes.Add(tnode);

                    }
                    return;
                }
                CreateTreeNodeFromXml(childNode, treeNode);
            }

        }
        private Dictionary<string, string> GetCollumns(XmlNodeList xml)
        {
            foreach (XmlNode xmlNode in xml)
            {
                if (xmlNode.Name == "Columns")
                {
                    var columns = new Dictionary<string, string>();
                    foreach (XmlNode xChild in xmlNode.ChildNodes)
                    {
                        string nodeValue = string.Empty;
                        foreach (XmlNode node in xChild.ChildNodes)
                        {
                            if (node.Name == "Caption")
                            {
                                nodeValue = node.Value;
                                break;
                            }
                        }
                        columns.Add(xChild.Name, nodeValue);
                    }
                    return columns;
                }

                return GetCollumns(xmlNode.ChildNodes);
            }
            return null;
        }

        private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void buttonSaveSetting_Click(object sender, EventArgs e)
        {
            foreach (var treeNode in deleteCol)
            {
                SetLayoutFromTreeNodes(treeNode,xmlDocument.DocumentElement);

            }
            Layout.LayoutString = xmlDocument.InnerXml;
           
            GridEx.Layouts.Clear();
            GridEx.Layouts.Add(Layout);
            ParentControl.GridEx = GridEx;
        }

        private bool removeFromLayout = false;
        private void SetLayoutFromTreeNodes(XmlNode treeNode,XmlElement xmlDocumentNode)
        {
            
            foreach (var xml in xmlDocumentNode)
            {
                if (((XmlElement)xml).Name=="Columns")
                {
                    List<XmlNode> xmlNodes=new List<XmlNode>();
                    for (int i=0;i<(((System.Xml.XmlNode)(xml))).ChildNodes.Count;i++)
                    {
                        if((((System.Xml.XmlNode)(xml))).ChildNodes[i].Name!=treeNode.Name)
                            xmlNodes.Add((((System.Xml.XmlNode)(xml))).ChildNodes[i]);
                    }
                   (((System.Xml.XmlNode)(xml))).RemoveAll();
                    removeFromLayout = true;
                   foreach (XmlNode xmlNode in xmlNodes)
                       (((System.Xml.XmlNode) (xml))).AppendChild(xmlNode);
                    return;
                }
                if (removeFromLayout)
                    break;
              SetLayoutFromTreeNodes(treeNode,(XmlElement) xml);
            }

        }
    }
}